package com.gameLogic.scene
{
	import com.gameLogic.utils.ObjDictionary;
	
	import laya.d3.core.Camera;
	import laya.d3.core.Sprite3D;
	import laya.d3.core.scene.Scene;
	import laya.display.Node;
	import laya.display.Sprite;
	import laya.maths.Point;

	public class SceneManager
	{
		
		private var _camera:Camera2D;
		public function get camera():Camera2D
		{
			return _camera;
		}
		
		private var _camera3d:Camera;

		public function get camera3d():Camera
		{
			return _camera3d;
		}

		
		private var _scene:Sprite;
		public function get scene():Sprite
		{
			return _scene;
		}
		
		private var _scene3d:Scene;
		public function get scene3d():Scene
		{
			return _scene3d;
		}

		private var _container3d:Sprite3D;
		
		private var _layer_map:Sprite;
		
		private var _layer_actor:Sprite;
		
		private var _layer_effect:Sprite;
		
		private var _layer_tip:Sprite;
		
		private var _layer_debug:Sprite;
		
		private var _layerDic:ObjDictionary;
		
		public function SceneManager()
		{
			
		}
		
		public function init():void
		{
			init2dScene();
		}
		
		private function init2dScene():void
		{
			_scene = new Sprite();
			_layer_map = new Sprite();
			_layer_actor = new Sprite;
			_layer_effect = new Sprite;
			_layer_debug = new Sprite;
			_layer_tip = new Sprite();
			_layer_debug.mouseEnabled = false;
			_layerDic = new ObjDictionary(Sprite);
			_layerDic.add(LayerEnum.MapLayer, _layer_map);
			_layerDic.add(LayerEnum.ActorLayer, _layer_actor);
			_layerDic.add(LayerEnum.EffectLayer, _layer_effect);
			_layerDic.add(LayerEnum.DebugLayer, _layer_effect);
			_layerDic.add(LayerEnum.TipLayer, _layer_tip);
			
			_scene.addChild(_layer_map);
			_scene.addChild(_layer_actor);
			_scene.addChild(_layer_effect);
			_scene.addChild(_layer_debug);
			_scene.addChild(_layer_tip);
			Laya.stage.addChild(_scene);
			_camera = new Camera2D(_scene);
		}
		
		public function addToLayer(spr:Sprite, layer:String=LayerEnum.MapLayer, posX:Number=0, posY:Number=0):void
		{
			var layerSpr:Sprite = _layerDic.get(layer) as Sprite;
			if(layerSpr)
			{
				layerSpr.addChild(spr);
				spr.x = posX;
				spr.y = posY;
			}
			else
			{
				trace("can not find layer " + layer);				
			}
		}
		
		public function getLayer(layer:String):Sprite
		{
			return  _layerDic.get(layer);
		}
		
		public function add3dContent(node:Node):void
		{
			_container3d.addChild(node);
		}
		
		public function getGlobalScreenPos():Point
		{
			if(_scene)
				return _scene.localToGlobal(new Point(_scene.mouseX, _scene.mouseY));
			else
				return new Point();
		}
		
		public function getMousePos():Point
		{
			if(_scene)
				return new Point(_scene.mouseX, _scene.mouseY);
			else
				return new Point();
		}
		
		public function update():void
		{
			_camera.update();
		}
		
		private static var _instance:SceneManager
		public static function get instance():SceneManager
		{
			if(!_instance){ _instance = new SceneManager(); }
			return _instance;
		}
		
	}
}